<?php
/**
 * 脚本订单备份
 */
class Order_bak_mdl extends MY_Model{
	
	public function __construct(){
		parent::__construct();		
	}
	
	/**
	 * 将旧订单备份
	 */
	public function orderBak($tableNameBak=null,$tableName=null){
		
		if ( empty($tableNameBak) || empty($tableName) ){
			return false;
		}
		
		$now = strtotime(date('Y-m-d',time()));//当前时间戳
		$halfYearsAgo = $now - 15552000;//半年前时间戳
		$limit = 10000;//一次处理一万
	
		//1、先查询旧订单最后订单id
		$bakMaxId = $this->db->select('id,oid')->from($tableNameBak)->order_by('id','DESC')->limit(1)->get()->row_array();
		
		$this->db->trans_begin();
		
// 		//id是否为空
// 		if ( !empty($bakMaxId) ){
			
// 			//查询半年前订单数据
// 			$oldOrderCount = (int)$this->db->select('count(*) as total')->where(array('createTime <'=>$halfYearsAgo,'id >'=>$bakMaxId['oid']))->from($tableName)->get()->row()->total;
			
// 			//无数据表示不需要备份
// 			if ( empty($oldOrderCount) ){
// 				return true;
// 			}
			
// 			$times = ceil($oldOrderCount/$limit);//需要循环几次
			
// 			for ( $i=0;$i<$times;$i++ ){
				
// 				echo $i."\n";
// 				$sqlIn = 'INSERT INTO '. $tableNameBak .' (oid,userID,isFinish,type,createTime,updateTime,takeSec,price,amount,phoneID,devID,schoolID,mode,isCached,lon,lat,mid,uuid,isValid) '.
// 						'SELECT id,userID,isFinish,type,createTime,updateTime,takeSec,price,amount,phoneID,devID,schoolID,mode,isCached,lon,lat,mid,uuid,isValid FROM '.$tableName.' WHERE id >'.$bakMaxId['oid'].' LIMIT '.$limit;
// 				$inRes = $this->db->query($sqlIn);
				 
// 				if ( $inRes ) {
// 					$sqlDe = 'DELETE FROM '.$tableName.' LIMIT ' . $limit;
// 					$deRes = $this->db->query($sqlDe);
// 				}
				 
// 				if ($this->db->trans_status() === FALSE){
// 					$this->db->trans_rollback();
// 					return false;
// 				}else{
// 					$this->db->trans_commit();
// 				}
				 
// 			}
			
// 		}else{
			
		   //查询半年前订单数据量
// 		   $oldOrderCount = (int)$this->db->select('count(*) as total')->where(array('createTime <'=>$halfYearsAgo))->from($tableName)->get()->row()->total;
		   
// 		   //无数据表示不需要备份
// 		   if ( empty($oldOrderCount) ){
// 		   		return true;
// 		   }
		   
		   //查询最大id
		   $oldOrderMaxId = $this->db->select('id')->where(array('createTime <'=>$halfYearsAgo))->from($tableName)->order_by('id','DESC')->get()->row();
		   
		   if ( empty($oldOrderMaxId) ){
		  		return true;
		   }
		  
		   $oldOrderMaxId = (int)($oldOrderMaxId->id);
		   
		   //查询当前id
		   $oldOrderBeginId = (int)$this->db->select('id')->where(array('createTime <'=>$halfYearsAgo))->from($tableName)->order_by('id','ASC')->get()->row()->id;
		   $needCount = $oldOrderMaxId - $oldOrderBeginId;
		   
		   $times = ceil($needCount/$limit);//需要循环几次
		   
		   $limitId = $oldOrderBeginId;
		   
		   for ( $i=0;$i<$times;$i++ ){
		   	
		   		echo $i."\n";
		   		$limitId += $limit;
		   		
		   		if ( $limitId>$oldOrderMaxId ){
		   			$limitId = $oldOrderMaxId;
		   		}
		   		
// 		   		echo $limitId."\n";

// 		   		$sqlIn = 'INSERT INTO '. $tableNameBak .' (oid,userID,isFinish,type,createTime,updateTime,takeSec,price,amount,phoneID,devID,schoolID,mode,isCached,lon,lat,mid,uuid,isValid) '.
// 				   	   'SELECT id,userID,isFinish,type,createTime,updateTime,takeSec,price,amount,phoneID,devID,schoolID,mode,isCached,lon,lat,mid,uuid,isValid FROM '.$tableName.' ORDER BY id LIMIT '.$limit;	
	
		   		$sqlIn = 'INSERT INTO '. $tableNameBak .' (oid,userID,isFinish,type,createTime,updateTime,takeSec,price,amount,phoneID,devID,schoolID,mode,isCached,lon,lat,mid,uuid,isValid) '.
		   				'SELECT id,userID,isFinish,type,createTime,updateTime,takeSec,price,amount,phoneID,devID,schoolID,mode,isCached,lon,lat,mid,uuid,isValid FROM '.$tableName.' WHERE id <='.$limitId.' ORDER BY id ';
		   		
		   		$inRes = $this->db->query($sqlIn);
		   		
		   		if ( $inRes ) {
// 		   			$sqlDe = 'DELETE FROM '.$tableName.' LIMIT ' . $limit;
		   			$sqlDe = 'DELETE FROM '.$tableName.' WHERE id <='.$limitId.' ORDER BY id ';
		   			$deRes = $this->db->query($sqlDe);
		   		}
		   		
		   		if ($this->db->trans_status() === FALSE){
		   			$this->db->trans_rollback();
		   			return false;
		   		}else{
		   			$this->db->trans_commit();
		   		}
		   		
		   		
		   }
		   
		  
		   
// 		}
		
		return true;
		
	}	
	
}